<template>

  <div>
    <div class="filter-container">
      <div class="filter-item">
        <span>任务名称:</span>
        <el-input v-model="listQuery.jobName" clearable type="primary" placeholder="请输入任务名称" @keyup.enter="handleFilter" />
      </div>
      <el-button v-permission="['task:schedule:job:refresh:job']" type="primary" class="filter-item" icon="Search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>
    </div>
    <div class="operationList">
      <div>
        <el-button v-permission="['task:schedule:job:add']" text type="primary" icon="Plus" @click="handleCreate">新增</el-button>
      </div>
      <div>
        <el-button v-permission="['task:schedule:job:refresh:job']" :loading="refreshTaskLoading" text type="primary" icon="RefreshRight" @click="handleRefreshTask">刷新</el-button>
      </div>
    </div>
    <el-table v-loading="listLoading" :row-class-name="tableRowClass" :data="list" fit border highlight-current-row header-cell-class-name="header-cell">
      <el-table-column label="序号" width="100" type="index" :index="calIndex"/>
      <el-table-column min-width="150" label="任务名称">
        <template #default="scope">
          <span>{{ scope.row.jobName }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="120" label="任务分组">
        <template #default="scope">
          <span>{{ scope.row.jobGroup }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="100" label="执行类">
        <template #default="scope">
          <span>{{ scope.row.executeClass }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="方法名">
        <template #default="scope">
          <span>{{ scope.row.methodName }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="100" label="执行表达式">
        <template #default="scope">
          <span>{{ scope.row.cronExpression }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="任务状态">
        <template #default="scope">
          <span>{{ dictLabel(scope.row.jobStatus, 'sf') }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="是否同步">
        <template #default="scope">
          <span>{{ dictLabel(scope.row.isConcurrent, 'sf') }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="300" label="操作">
        <template #default="scope">
          <el-button v-if="scope.row.jobStatus==='0'" v-permission="['task:schedule:job:change:job:status']" type="primary" plain size="small" @click="handleChangeJobStatus(scope.row, 'start', '启动')">开始</el-button>
          <el-button v-if="scope.row.jobStatus==='1'" v-permission="['task:schedule:job:change:job:status']" type="primary" plain size="small" @click="handleChangeJobStatus(scope.row, 'stop', '停止')">停止</el-button>
          <el-button v-permission="['task:schedule:job:refresh:job']" size="small" type="primary" plain @click="handleRefresh(scope.row)">刷新</el-button>
          <el-button v-permission="['task:schedule:job:change:job:status']" size="small" type="primary" plain @click="runAJobNow(scope.row)">执行一次</el-button>
          <el-button v-permission="['task:schedule:job:detail']" size="small" plain type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['task:schedule:job:delete']" size="small" plain type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" v-model:page="listQuery.page" v-model:limit="listQuery.limit" :total="total" :page-sizes="pageArray" @pagination="getList" />

    <taskListForm ref="taskListForm" @refresh-list="getList"/>
  </div>
</template>

<script>
import { fetchScheduleList, deleteSchedule, refreshTask, refreshSchedule, changeJobStatus, runAJobNow } from '@/api/tool/task/schedule'
import Pagination from '@/components/Pagination/index.vue'
import taskListForm from './taskListForm.vue'
export default {
  name: 'TaskList',
  components: { Pagination, taskListForm },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      refreshTaskLoading: false,
      batchDeleteLoading: false,
      pageArray: this.$store.dictStore.pageArray,
      listQuery: {
        page: 1,
        limit: this.$store.dictStore.defaultPageSize,
        importance: undefined,
        title: undefined,
        type: undefined
      },
      showReviewer: false,
      temp: {
        id: undefined,
        jobName: '',
        cronExpression: '',
        executeClass: '',
        methodName: '',
        methodParams: '',
        misfirePolicy: '',
        loadWay: '1',
        isConcurrent: '1',
        description: '',
        jobStatus: '',
        jobGroup: ''
      },

      dialogPvVisible: false,
      pvData: [],

      downloadLoading: false,
      multipleSelection: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 获取列表
     */
    getList(loading = true) {
      this.listLoading = loading
      fetchScheduleList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 修改状态
     * @param row 选中数据
     * @param status 状态
     */
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },

    /**
     * 新增
     */
    handleCreate() {
      this.$refs.taskListForm.handleCreate()
    },

    /**
     * 修改
     * @param row 选中数据
     */
    handleUpdate(row) {
      this.$refs.taskListForm.handleUpdate(row)
    },

    /**
     * 删除
     * @param row 选中数据
     */
    handleDelete(row) {
      this.$confirm('确认要删除任务么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.$delAnim = true
        deleteSchedule([row.id]).then(response => {
          const data = response.data
          if (data.success) {
            row.$delAnim = true
            this.getList(false)
            this.$message.success('删除成功')
          } else {
            this.getList(false)
            this.$message.error(data.msg)
          }
        })
      })
    },
    /**
     * 更新任务
     * @param row 选中数据
     */
    handleRefresh(row) {
      this.$confirm('确认要更新任务么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        refreshSchedule(row.id).then(response => {
          const data = response.data
          if (data.success) {
            this.$message.success(data.msg)
            this.getList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    /**
     * 执行任务
     * @param row 选中数据
     */
    runAJobNow(row) {
      this.$confirm('确认要执行一次任务么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        runAJobNow(row).then(response => {
          const data = response.data
          if (data.success) {
            this.$message.success('执行成功')
            this.getList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    /**
     *
     * @param row 选中数据
     * @param cmd 操作编码
     * @param cmdLabel 操作
     */
    handleChangeJobStatus(row, cmd, cmdLabel) {
      this.$confirm('你确定要' + cmdLabel + '该计划任务么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        changeJobStatus(row.id, cmd).then(response => {
          const data = response.data
          if (data.success) {
            this.$message.success(data.msg)
            this.getList()
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    /**
     * 刷新任务
     */
    handleRefreshTask() {
      refreshTask().then(() => {
        this.$message.success('提交成功')
        this.$refs.multipleTable.clearSelection()
        this.refreshTaskLoading = false
      }).catch(() => {
        this.refreshTaskLoading = false
      })
    }
  }
}
</script>

